{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "02b782b6-0d2b-4904-a287-7dd23d6ef66f",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Robodyno PWM API"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d01b0728-7e31-4bfc-b205-b7bd48abc0d5",
   "metadata": {},
   "source": [
    "## 1.初始化PWM对象"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "dce58745-a428-4c2e-926f-4dc4eda53f1f",
   "metadata": {},
   "source": [
    "`PwmDriver(can, id_)`\n",
    "\n",
    "参数：\n",
    "- `can` : can总线接口\n",
    "- `id_` : PWM驱动板模块ID，默认`0x21`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "16a0fe4e-7655-435b-82b1-b556652acad6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from robodyno.components import PwmDriver\n",
    "from robodyno.interfaces import CanBus\n",
    "can = CanBus()\n",
    "pwm = PwmDriver(can, 0x21)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "73d85177-0512-4cb3-ae26-396b2571be57",
   "metadata": {},
   "source": [
    "## 2.读取PWM设备版本"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c736a6d1-f06e-4f97-8060-14a2566b2aa6",
   "metadata": {},
   "source": [
    "`get_version(timeout = 0)`\n",
    "\n",
    "参数：\n",
    "\n",
    "- timeout : 请求超时时间(s)，0代表无超时时间\n",
    "\n",
    "返回值 ：\n",
    "\n",
    "- API版本dict\n",
    "    - `main_version` : 主版本号\n",
    "    - `sub_version` : 副版本号\n",
    "    - `type` : 设备类型\n",
    "        - ROBODYNO_PWM_DRIVER"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "44fc23c3-23a2-4280-b2ac-ddbf425473e3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'main_version': 0, 'sub_version': 3, 'type': <Model.ROBODYNO_D_VAC01: 97>}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pwm.get_version()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "dcf22194-3a1b-4e01-b6c6-9e707c896ced",
   "metadata": {},
   "source": [
    "## 3.设置CAN_ID"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c98e4edc-338b-423b-b89a-76691c32a4d5",
   "metadata": {},
   "source": [
    "`config_can_bus(new_id)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `new_id` ：PWM 设备的新CAN_ID (默认：0x21)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "941c8c07-526d-4a13-ac96-17e19beb4fcd",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "pwm.config_can_bus(new_id = 0x21)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b85ab2bc-ee16-4f20-aa5c-780951412ccc",
   "metadata": {},
   "source": [
    "## 4.开启PWM输出"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "45f37768-5f04-4369-b368-e9f1996b827b",
   "metadata": {},
   "source": [
    "`on()`\n",
    "\n",
    "- 打开PWM输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7ee67011-66be-4f09-a50c-934d78bbf0a5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "pwm.on()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ce5754bc-972e-4fc6-807c-63fefdd0048c",
   "metadata": {},
   "source": [
    "## 5.关闭PWM输出"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e00662f8-570a-4b03-8111-82e73b5f3cf7",
   "metadata": {},
   "source": [
    "`off()`\n",
    "\n",
    "- 关闭PWM输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5a6161c3-e292-4216-b897-95f65ec52698",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "pwm.off()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "4913c5fd-eec8-45c2-b912-272f27401011",
   "metadata": {},
   "source": [
    "## 6.设置伺服电机位置"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6f080de0-c4fc-4a43-b115-4673edae20db",
   "metadata": {},
   "source": [
    "`set_servo(pos)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `pos` ：伺服电机位置 (范围：0～255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1da48898-2841-40ab-afb2-023c1ed9d090",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "pwm.set_servo(0)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "17db9cf9-83e7-4555-ad88-6ad1dc3cdb07",
   "metadata": {},
   "source": [
    "## 7.设置PWM"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "413bd25f-7851-4718-bb3c-85a8621b5658",
   "metadata": {},
   "source": [
    "`set_pwm(pwm)`\n",
    "\n",
    "参数 ：\n",
    "\n",
    "- `pwm` ：pwm的值 (范围：0～255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "184e2e87-2543-4281-807f-683f0688cbec",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "pwm.set_pwm(0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
